package com.spzx.product.controller;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.common.security.utils.SecurityUtils;
import com.spzx.product.api.domain.Product;
import com.spzx.product.domain.dto.ProductDTO;
import com.spzx.product.domain.vo.ProductVO;
import com.spzx.product.service.ProductService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@Tag(name = "商品管理模块")
@RequestMapping("/product")
public class ProductController extends BaseController {

    @Resource
    private ProductService productService;

    @Operation(summary = "条件查询商品分页列表")
    @GetMapping("/list")
    public TableDataInfo list(Product product) {
        //使用mp的分页查询实现
        Page<ProductVO> page=productService.listpage(product);
        return getDataTable(page);
    }

    @Operation(summary = "新增商品")
    @PostMapping
    public AjaxResult save(@RequestBody ProductDTO productDTO) {
        return toAjax(productService.saveProductDTO(productDTO));
    }

    @Operation(summary = "修改前：查询商品详情数据")
    @GetMapping("/{id}")
    public AjaxResult getById(@PathVariable("id") Long id) {
        return success(productService.getProductDTOById(id));
    }

    @Operation(summary = "更新商品")
    @PutMapping
    public AjaxResult update(@RequestBody ProductDTO productDTO) {
        return toAjax(productService.updateProductDTO(productDTO));
    }

    @Operation(summary = "删除商品")
    @DeleteMapping("/{ids}")
    public AjaxResult delete(@PathVariable("ids") List<Long> ids) {
        //商品数据存到四张表中
        return toAjax(productService.removeBatchSpusById(ids));
    }

    @Operation(summary = "商品审批")
    @PutMapping("updateAuditStatus/{id}/{auditStatus}")
    public AjaxResult updateAuditStatus(@PathVariable("id") Long id,
                                        @PathVariable("auditStatus") Integer auditStatus) {
        return toAjax(productService.update(Wrappers.lambdaUpdate(Product.class)
                .eq(Product::getId, id)
                .set(Product::getAuditStatus, auditStatus)
                .set(Product::getUpdateBy, SecurityUtils.getUsername())
                .set(Product::getAuditMessage,auditStatus==1?"审批通过":"审批不通过")));
    }

    @Operation(summary = "商品上下架")
    @PutMapping("updateStatus/{id}/{status}")
    public AjaxResult updateStatus(@PathVariable("id") Long id, @PathVariable("status") Integer status) {
        return toAjax(productService.updateStatus(id, status));
    }
}
